import numpy as np
import pandas as pd
import os
import sys
import pickle
import json
import time
from collections import defaultdict
from pytz import timezone
from datetime import datetime
from sys import getsizeof as getsize
import networkx as nx


def binary_conversion(var: int):
    """
    二进制单位转换
    :param var: 需要计算的变量，bytes值
    :return: 单位转换后的变量，kb 或 mb
    """
    assert isinstance(var, int)
    if var <= 1024:
        return f'占用 {round(var / 1024, 2)} KB内存'
    else:
        return f'占用 {round(var / (1024 ** 2), 2)} MB内存'

    
def print_json(json_str):
    print(json.dumps(json_str, indent=2))


def save_json(save_path,data):
    assert save_path.split('.')[-1] == 'json'
    with open(save_path,'w') as file:
        json.dump(data,file)


def load_json(file_path):
    assert file_path.split('.')[-1] == 'json'
    with open(file_path,'r') as file:
        data = json.load(file)
    return data


def save_pkl(save_path, data):
    assert save_path.split('.')[-1] == 'pkl'
    with open(save_path,'wb') as file:
        pickle.dump(data,file)


def load_pkl(file_path):
    assert file_path.split('.')[-1] == 'pkl'
    with open(file_path,'rb') as file:
        data = pickle.load(file)
    return data